const Router = require('koa-router');
const router = new Router();
const {db, QueryTypes} = require("../../models/db");

// 关联的退货打印小票
router.post('/deliver/getBackRel', async ctx => {
    let tenantId = ctx.user.tenantId;
    let billId = ctx.request.body.billId;
    let real = `select c.buName,c.billNo,a.id,a.invId,a.invName,a.spec,a.calNum,a.qty,a.unit1,a.unit2 from 
     v_main_info a inner join (select tenantId,iid from invoice_back where tenantId=${tenantId} and oldIid=${billId}) b 
on a.tenantId=b.tenantId and a.iid=b.iid 
inner join v_main c on a.tenantId=c.tenantId and a.iid=c.id 

`
    let list = await db.query(real, {type: QueryTypes.SELECT})
    if(list.length === 0) return ctx.err('没有该票据对应的退货单据')
    return ctx.suc('', list)

})

module.exports = router;
